package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/17.
 */

public class CashActivity extends BaseActivity {
    private RequestQueue requestQueue;
    private EditText ed_amout,ed_bankNumber,ed_bankName,ed_name,ed_bankMobile;
    private Button btn;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("提现");
        userId=PreferenceUtil.getString(getApplicationContext(),"userId");
        requestQueue= Volley.newRequestQueue(getApplication());
        ed_amout= (EditText) findViewById(R.id.cash_ed_amout);
        ed_bankName= (EditText) findViewById(R.id.cash_ed_bank);
        ed_bankNumber= (EditText) findViewById(R.id.cash_ed_bankNumber);
        ed_name= (EditText) findViewById(R.id.cash_ed_name);
        ed_bankMobile= (EditText) findViewById(R.id.cash_ed_bankMobile);
        btn= (Button) findViewById(R.id.cash_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ed_amout.getText().toString().isEmpty()){
                    if(!ed_bankName.getText().toString().isEmpty()){
                        if(!ed_bankNumber.getText().toString().isEmpty()){
                            if (!ed_bankMobile.getText().toString().isEmpty()){
                                if(!ed_name.getText().toString().isEmpty()){
                                  if (!userId.isEmpty()){
                                      getCash();
                                  }
                                   else {
                                      Toast.makeText(CashActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                                  }
                                }
                               else{
                                    Toast.makeText(CashActivity.this, "开户人姓名不能为空", Toast.LENGTH_SHORT).show();
                                }
                            }
                            else{
                                Toast.makeText(CashActivity.this, "银行预留手机号不能为空", Toast.LENGTH_SHORT).show();
                            }

                        }else{
                            Toast.makeText(CashActivity.this, "银行卡号不能为空", Toast.LENGTH_SHORT).show();
                        }

                    }else {
                        Toast.makeText(CashActivity.this, "开户行不能为空", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(CashActivity.this, "金额不能为空", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void getCash() {
        StringRequest request = new StringRequest(Request.Method.POST, Url.GetCash,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("提现界面", "提现接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                Toast.makeText(CashActivity.this, "提现申请提交成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            else if(jsonObject.getString("code").equals("01")){
                                Toast.makeText(CashActivity.this, "提现超额", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(CashActivity.this, "提现失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(CashActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("loginId", userId);
                map.put("cashAmount", ed_amout.getText().toString());
                map.put("bankNumber", ed_bankNumber.getText().toString());
                map.put("bankName", ed_bankName.getText().toString());
                map.put("bankUser", ed_name.getText().toString());
                map.put("bankMobile", ed_bankMobile.getText().toString());
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
